﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcT4ViewEngine.Lib.Views;

namespace MvcT4ViewEngine.Lib.ViewEgine
{

    /// <summary>
    /// A composite view engine to help plugging view engines
    /// </summary>
    public class CompositeViewEngine : VirtualPathProviderViewEngine
    {
        // Ctor - Let us set all location formats
        public CompositeViewEngine()
        {
            base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.master", 
                                                        "~/Views/Shared/{0}.master" };
            base.AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.master", 
                                                         "~/Areas/{2}/Views/Shared/{0}.master" };
            base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.view.tt", 
                                                      "~/Views/{1}/{0}.aspx", "~/Views/{1}/{0}.ascx",  
                                                      "~/Views/Shared/{0}.view.tt", 
                                                      "~/Views/Shared/{0}.aspx", "~/Views/Shared/{0}.ascx" };
            base.AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.view.tt", 
                                                       "~/Areas/{2}/Views/{1}/{0}.aspx", "~/Areas/{2}/Views/{1}/{0}.ascx",
                                                       "~/Areas/{2}/Views/Shared/{0}.view.tt", 
                                                       "~/Areas/{2}/Views/Shared/{0}.aspx", "~/Areas/{2}/Views/Shared/{0}.ascx" };
            base.PartialViewLocationFormats = base.ViewLocationFormats;
            base.AreaPartialViewLocationFormats = base.AreaViewLocationFormats;
        }

        /// <summary>
        /// Handle the creation of a partial view
        /// </summary>
        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            if (partialPath.EndsWith(".view.tt"))
                return new T4View(partialPath);
            else
                return new WebFormView(partialPath, null);
        }

        /// <summary>
        /// Handle the creation of a view
        /// </summary>
        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            if (viewPath.EndsWith(".view.tt") && String.IsNullOrEmpty(masterPath))
            {
                return new T4View(viewPath);
            }
            else if (viewPath.EndsWith(".view.tt") && !String.IsNullOrEmpty(masterPath))
            {
                return new T4View(viewPath,masterPath);
            }
            else
                return new WebFormView(viewPath, masterPath);
        }

        /// <summary>
        /// Check if the file exists
        /// </summary>
        protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            return base.FileExists(controllerContext, virtualPath);
        }

       
    }



}